<template>

  <div class="app-container">
    <div><h2 style="text-align: center;">课程基本信息</h2></div>

    <el-form label-width="120px" style="padding: 30px 0;margin-right: 25%;margin-left: 25%;">
      <el-form-item label="课程标题">
        <el-input v-model="courseInfo.title" placeholder=" 示例：初级四级听力"/>
      </el-form-item>

      <!-- 所属分类 -->
      <el-form-item label="课程类别">
        <el-select v-model="courseInfo.subjectId">
          <el-option v-for="subject in subjectList" :key="subject.id" :label="subject.title" :value="subject.id"/>
        </el-select>
      </el-form-item>

      <!-- 课程简介-->
      <el-form-item label="课程简介">
        <el-input type="textarea" :rows="4" v-model="courseInfo.description" placeholder="请简述课程"/>
      </el-form-item>

      <!-- 课程封面-->
      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_API+'/aliyun-service/uploadImage'"
          class="avatar-uploader">
          <img :src="courseInfo.cover"
               @click="saveBtnDisabled=true">
        </el-upload>
      </el-form-item>

      <el-form-item label="课程价格">
        <el-input-number :min="0" v-model="courseInfo.price" controls-position="right"/>
        元
      </el-form-item>

      <el-form-item style="text-align: center">
        <el-button :disabled="saveBtnDisabled" plain="true" type="primary" @click="SaveOrUpdate">保存并下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import courseApi from '@/api/course'
  import subjectApi from '@/api/subject'

  export default {
    data() {
      return {
        saveBtnDisabled: false, // 按钮是否禁用
        courseInfo: {// 表单数据
          id: '',
          price: 0,
          // 以下解决表单数据不全时insert语句非空校验
          subjectId: '',//分类ID
          cover: 'https://rice-college.oss-cn-shenzhen.aliyuncs.com/2020/12/23/8ae4f73c592441fca8d243de87dd048c.jpg',
          description: ''
        },
        BASE_API: process.env.BASE_API, // 接口API地址
        subjectList: [] // 分类
      }
    },

    created() {
      if (this.$route.params.id != 0) {
        this.courseInfo.id = this.$route.params.id
        this.getInfoById()
      } else {
        this.courseInfo = {
          id: '',
          price: 0,
          // 以下解决表单数据不全时insert语句非空校验
          subjectId: '',//分类ID
          cover: 'https://rice-college.oss-cn-shenzhen.aliyuncs.com/2020/12/23/8ae4f73c592441fca8d243de87dd048c.jpg',
          description: ''
        }
      }
      //初始化一级分类
      this.getSubject()
    },

    methods: {
      //获取课程信息
      getInfoById() {
        courseApi.getCourseInfoById(this.courseInfo.id)
          .then(response => {
            this.courseInfo = response.data.item
          })
      },

      //查询所有课程分类
      getSubject() {
        subjectApi.getAllSubject()
          .then(response => {
            this.subjectList = response.data.list
          })
      },

      SaveOrUpdate() {
        if (this.courseInfo.id) {
          this.updateCourseInfo()
        } else {
          this.addCourseInfo()
        }
      },

      //添加课程
      addCourseInfo() {
        courseApi.addCourse(this.courseInfo)
          .then(response => {
            //提示信息
            this.$message({
              type: 'success',
              message: '添加课程信息成功'
            })
            this.$router.push({ path: '/course/chapterInfo/' + response.data.id })
          })
      },

      //修改课程
      updateCourseInfo() {
        courseApi.updateCourse(this.courseInfo)
          .then(response => {
            //提示信息
            this.$message({
              type: 'success',
              message: '修改课程信息成功'
            })
            this.$router.push({ path: '/course/chapterInfo/' + this.courseInfo.id })
          })
      },

      //上传封面之前
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      },

      //上传封面成功
      handleAvatarSuccess(res, file) {
        if (res.success) {
          this.courseInfo.cover = res.data.url
          // 强制重新渲染
          this.$forceUpdate()
          this.saveBtnDisabled = false
        } else {
          this.$message.error('上传失败')
        }
      }
    }
  }
</script>

<style scoped>
  .tinymce-container {
     line-height: 29px;
  }
</style>
